package com.woyowodaows.utils;

import android.content.Context;
import android.text.TextUtils;

import java.io.File;

/**
 * @author haiwei
 *         文件保存工具类
 *         2017/3/23
 */
public class FileUtils {

    /**
     * 缓存文件夹名称
     */
    public static String cacheFileName = "woyowodaowsCacheFile";
    /**
     * 缓存图片
     */
    private static String crashImageName = "woyowodaowsrashImage";
    /**
     * APP主目录路径
     */
    private static String filePath;

    /**
     * 初始化文件保存位置
     */
    public static void initAppFileFolder() {
        filePath = SDCardUtils.getSDCardPath() + "woyowodaows";
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdir();
        }
    }


    /**
     * 获取缓存文件夹路径(需要手动删除文件)
     *
     * @return
     */
    public static String getCacheFilePath() {
        String temFilePath = filePath + File.separator + cacheFileName;
        File file = new File(temFilePath);
        if (!file.exists()) {
            file.mkdir();
        }
        return file.getAbsolutePath();
    }

    /**
     * 获取缓存文件夹路径(需要手动删除文件)
     *
     * @return
     */
    public static File getCacheImageFile() {
        String temFilePath = filePath + File.separator + crashImageName;
        File file = new File(temFilePath);
        if (!file.exists()) {
            file.mkdir();
        }
        return file;
    }

    /**
     * 获取异常缓存路径
     *
     * @return
     */
    public static String getCrashFilePath() {
        String temFilePath = filePath + File.separator + cacheFileName;
        File file = new File(temFilePath);
        if (!file.exists()) {
            file.mkdir();
        }
        return file.getAbsolutePath();
    }

    /**
     * 删除文件
     *
     * @param path
     */
    public static void delImageWithPath(String path) {
        if (TextUtils.isEmpty(path)) {
            return;
        }
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 从磁盘清除缓存文件夹所有文件
     */
    public void cleanFileWithCache(Context context) {
        File cacheDirectory = getCacheImageFile();
        cleaFile(cacheDirectory);
    }


    /**
     * 删除文件夹内所有文件
     *
     * @param fileDirectory
     */
    public static void cleaFile(File fileDirectory) {
        if (fileDirectory.exists()) {
            File[] files = fileDirectory.listFiles();
            for (File file : files) {
                if (file != null) {
                    file.delete();
                }
            }
        }
    }


}
